package com.wry.service.impl;

import com.wry.entity.WOrder;
import com.wry.service.OrderProducerService;
import com.wry.service.WOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <h1>  <h1>
 *
 * @author 魏如元
 * @since 2022/10/14
 */
@Service
@Slf4j
public class OrderProducerServiceImpl implements OrderProducerService {

    @Resource
    private WOrderService orderService;

    /**
     * 订单状态已完成
     */
    public final int PAY_DONE = 1;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOrder(WOrder order) {
        // 默认没有支付
        order.setPayStatus(0);
        orderService.save(order);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePayStatusByOrderId(String orderId){
        orderService.lambdaUpdate()
                .eq(WOrder::getOrderId,orderId)
                .set(WOrder::getPayStatus, PAY_DONE)
                .update();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean checkOrderPaySuccess(String orderId){
        final WOrder order = orderService.lambdaQuery()
                .eq(WOrder::getOrderId, orderId)
                .eq(WOrder::getPayStatus, PAY_DONE)
                .one();
        return Objects.nonNull(order);
    }
}
